package com.zxiw.zxedu.recyclerview.listener

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.zxiw.zxedu.lib_recyclerview.R
import com.zxiw.zxedu.recyclerview.XRecyclerView
import com.zxiw.zxedu.recyclerview.adapter.HelperRecyclerViewDragAdapter


/**
 * @author zhangxiaoming
 * @date 2023/8/17
 * @description item拖动条监听回调
 */
class ItemDragListener(var mAdapter: HelperRecyclerViewDragAdapter<*>) :
    ItemTouchHelper.Callback() {
    var mMoveThreshold = 0.1f
    var mSwipeThreshold = 0.7f

    var mDragMoveFlags =
        ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
    var mSwipeMoveFlags = ItemTouchHelper.END

    override fun isLongPressDragEnabled(): Boolean {
        return false
    }

    override fun isItemViewSwipeEnabled(): Boolean {
        return mAdapter.isItemSwipeEnable()
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG
            && !isViewCreateByAdapter(viewHolder!!)
        ) {
            mAdapter.onItemDragStart(viewHolder)
            viewHolder.itemView.setTag(R.id.BaseQuickAdapter_dragging_support, true)
        }
        super.onSelectedChanged(viewHolder, actionState)
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        if (isViewCreateByAdapter(viewHolder)) {
            return
        }
        if (viewHolder.itemView.getTag(R.id.BaseQuickAdapter_dragging_support) != null
            && viewHolder.itemView.getTag(R.id.BaseQuickAdapter_dragging_support) as Boolean
        ) {
            mAdapter.onItemDragEnd(viewHolder)
            viewHolder.itemView.setTag(R.id.BaseQuickAdapter_dragging_support, false)
        }
    }

    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        if (isViewCreateByAdapter(viewHolder)) {
            return makeMovementFlags(0, 0);
        }
        return makeMovementFlags(mDragMoveFlags, mSwipeMoveFlags)
    }

    override fun onMove(
        recyclerView: RecyclerView,
        source: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        return source.itemViewType == target.itemViewType
    }

    override fun onMoved(
        recyclerView: RecyclerView,
        source: RecyclerView.ViewHolder,
        fromPos: Int,
        target: RecyclerView.ViewHolder,
        toPos: Int,
        x: Int,
        y: Int
    ) {
        mAdapter.onItemDragMoving(source, target)
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

    }

    override fun getMoveThreshold(viewHolder: RecyclerView.ViewHolder): Float {
        return mMoveThreshold
    }

    override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
        return mSwipeThreshold
    }
    private fun isViewCreateByAdapter(viewHolder: RecyclerView.ViewHolder): Boolean {
        val type = viewHolder.itemViewType
        return type == XRecyclerView.TYPE_REFRESH_HEADER || type == XRecyclerView.TYPE_LOADMORE_FOOTER
    }
}